import { defineStore } from 'pinia'
import { getToken, setToken, removeToken } from '@/utils/cookie'
import Token from '@/api/sys/token'
import User from '@/api/sys/user'
import Dict from '@/api/sys/dict'
import { DictItem } from '@/utils'

const DictStore = defineStore({
  id: 'dict',
  state: () => ({
    // 字典值
    dict: null,
    // 字典名
    dictLabel: null,
  }),
  actions: {
    /**
     *  初始化字典数据
     */
    async initDict() {
      let { result } = await Dict.getAllDict()
      let dictMap = new Map()
      let dictLabelMap = new Map()
      result.forEach((d) => {
        let itemMap = new Map()
        d.dictItems?.forEach((item) => {
          itemMap.set(item.value, new DictItem(item.label, item.metaEntity))
        })
        dictMap.set(d.dictCode, itemMap)
        dictLabelMap.set(d.dictCode, d.dictName)
      })
      this.dict = dictMap
      this.dictLabel = dictLabelMap
    },

    getByDictCode(dictCode) {
      return this.dict.get(dictCode)
    },
  },
})

export default DictStore
